// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of CUIImplRepository.
//   $Id$


#include "stdafx.h"
#include "uiimplrepository.h"

#include <XBLEngine_i.c>
#include <XBLResolver_i.c>

CUIImplRepository::CUIImplRepository(const wchar_t * wsChromeDirectory,IXMLDOMNodeList * ,IXSLProcessor * ,IDOMImplementation * pImpl)
	:m_sChromeDirectory(wsChromeDirectory)
{
	m_pResolver.CoCreateInstance(CLSID_BindingResolver);
	if(m_pResolver)
	{
		
		m_pResolver->AddSchemeDirectory(L"chrome",m_sChromeDirectory);
		CComBSTR sMappingsDoc(m_sChromeDirectory.Copy());
		sMappingsDoc +=L"bindings.xml";
		m_pResolver->Init(sMappingsDoc,pImpl);
	}
	
}


CUIImplRepository::~CUIImplRepository(void)
{
}

HRESULT CUIImplRepository::AddBindingsDocument(BSTR bsURL)
{
	HRESULT hr;
	if(m_pResolver)
	{
		hr = m_pResolver->LoadAdditionalMappings(bsURL);
	}
	else
	{
		hr = E_PENDING;
	}
	return hr;
}

HRESULT CUIImplRepository::GetUIImpl(IXMLDOMNode * pSelectionNode, IEngine ** ppRetVal)  
{
	
	CComPtr<IUnknown> pUnk;
	HRESULT hr = m_pResolver->GetBinding(pSelectionNode,&pUnk);
	if(pUnk)
	{
		hr = pUnk->QueryInterface(IID_IEngine,reinterpret_cast<void**>(ppRetVal));
	}
#ifdef _DEBUG
	else
	{
		if(false)
		{
			CComBSTR s;
			pSelectionNode->get_xml(&s);
			::MessageBoxW(0,s,0,0);
		}
	}
#endif
	return hr;
}

void GetLastErrorDescription(CComBSTR & sDescription)
{
	CComPtr<IErrorInfo> pErr;
	::GetErrorInfo(0,&pErr);
	if(pErr)
		pErr->GetDescription(&sDescription);
	if(!sDescription)
		sDescription = L"[Unknown Error]";
}

// <summary>Gets the collection of namespaces that the XBL bindings that will be returned by this repository use</summary><param name="pRetval">on return, contains the list of namespaces</param>
HRESULT CUIImplRepository::GetXBLNamespaces(IXMLDOMNodeList ** ppRetval)
{
	HRESULT hr = E_POINTER;
	
	if(ppRetval)
	{
		if(m_pResolver)
		{
			hr = m_pResolver->get_selectionNamespaces(ppRetval);
		}
		else
		{
			hr = E_PENDING;
		}
	}

	return hr;
}

